package com.example.mymvvmdemo.models.add_device.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.models.add_device.adapter.DeviceTypeAdapter;
import com.example.mymvvmdemo.models.add_device.entity.DeviceTypeListEntity;
import com.example.mymvvmdemo.utils.LogUtils;

import java.util.ArrayList;


public class SwitchDeviceTypeDialog implements DeviceTypeAdapter.DeviceTypeListener {

    private Context context;
    //控件
    private TextView tvTitle;
    private ImageView ivClose;
    private RecyclerView recyclerView;
    private DeviceTypeAdapter deviceTypeAdapter;
    private ArrayList<DeviceTypeListEntity.DataDTO.DeviceTypesDTO> list = new ArrayList<>();
    //弹出框
    private Dialog dialog;

    private PromptClickListener listener;

    public void setListener(PromptClickListener listener) {
        this.listener = listener;
    }

    public SwitchDeviceTypeDialog(Context context) {
        this.context = context;
        try {
            // 首先得到整个View
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_switch_device_type, null);

            tvTitle = view.findViewById(R.id.tv_title);
            ivClose = view.findViewById(R.id.iv_close);
            recyclerView = view.findViewById(R.id.recycler_view);

            /**------------设置文本---------------------------*/

            dialog = new Dialog(context, R.style.DarkBackgroundDialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();//获取Dialog的窗口
            //实例化窗口布局对象
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽度铺满
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;//高度自适应
            lp.gravity = Gravity.CENTER;//窗口停靠在底部居中
            dialogWindow.setAttributes(lp);

            ivClose.setOnClickListener(view1 -> {
                close();
            });

            initAdapter();

        } catch (Exception e) {
            LogUtils.e("WifiConfirmDialog error:" + e.getMessage());
        }
    }

    private void initAdapter() {
        deviceTypeAdapter = new DeviceTypeAdapter(context, list);
        deviceTypeAdapter.setListener(this);
        recyclerView.setAdapter(deviceTypeAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

    }

    public void setList(ArrayList<DeviceTypeListEntity.DataDTO.DeviceTypesDTO> list) {
        this.list = list;
        deviceTypeAdapter.setDataList(list);
        if (deviceTypeAdapter != null) {
            deviceTypeAdapter.notifyDataSetChanged();
        }
    }

    public String getType() {
        for (int i = 0; i < list.size(); i++) {
            DeviceTypeListEntity.DataDTO.DeviceTypesDTO devicesDTO = list.get(i);
            boolean select = devicesDTO.isSelect();
            if (select) {
                return devicesDTO.getCode();
            }
        }
        return null;
    }

    public void setTitle(String str) {
        tvTitle.setText(str);
    }

    public void setTitle(int str) {
        tvTitle.setText(str);
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void close() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onClickTypeListener(int position) {
        for (int i = 0; i < deviceTypeAdapter.getDataListSize(); i++) {
            DeviceTypeListEntity.DataDTO.DeviceTypesDTO deviceTypesDTO = deviceTypeAdapter.getDataList().get(i);
            deviceTypesDTO.setSelect(position == i);
        }
        deviceTypeAdapter.notifyDataSetChanged();

        DeviceTypeListEntity.DataDTO.DeviceTypesDTO deviceTypesDTO = deviceTypeAdapter.getDataList().get(position);
        String code = deviceTypesDTO.getCode();
        String name = deviceTypesDTO.getName();
        if (listener != null) {
            listener.onConfirmClick(code, name);
        }

    }

    public interface PromptClickListener {
        void onCancelClick();

        void onConfirmClick(String code, String name);
    }

}
